package com.itfitness.myapplication;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2022/3/3 16:00
 * @UpdateUser: 更新者：
 * @UpdateDate: 2022/3/3 16:00
 * @UpdateRemark: 更新说明：
 */
public class HookMsg implements IXposedHookLoadPackage {
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if(lpparam.packageName.equals("com.ximalaya.ting.android")){
            XposedHelpers.findAndHookMethod("com.ximalaya.ting.android.host.activity.login.LoginActivity",
                    lpparam.classLoader,
                    "onResume", new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);
                        }

                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                            Window window = (Window) XposedHelpers.callMethod(param.thisObject,"getWindow");
                            ViewGroup decorView = (ViewGroup) window.getDecorView();
                            listTextView(decorView);
                        }
                    });
        }
    }

    /**
     * 遍历所有的TextView
     * @param viewGroup
     */
    private void listTextView(ViewGroup viewGroup){
        for (int i = 0 ; i < viewGroup.getChildCount() ; i++){
            View childAt = viewGroup.getChildAt(i);
            if(childAt instanceof ViewGroup){
                listTextView((ViewGroup) childAt);
            }else if(childAt instanceof TextView){
                TextView textView = (TextView) childAt;
                String text = textView.getText().toString();
                XposedBridge.log(textView.getText().toString());
                if(text.equals("登录发现更多精彩")){
                    textView.setText("关注Itfitness");
                }
            }
        }
    }
}
